<template>
  <Modal
    v-model="form.visible"
    :width="1000"
    :mask-closable="false"
    :loading="loading"
    :title="form.data.id ? '修改费用' : '新增费用'"
    ok-text="提交"
    @on-ok="ok">
    <Row class="mt30">
      <Col span="16" offset="4">
        <Form ref="form" :model="form.data" :rules="ruleValidate" :label-width="110">
          <FormItem prop="costType" label="费用类型">
            <Select v-model="form.data.costType" style="width: 100%" placeholder="请选择费用用途">
              <Option
                v-for="item in costTypeCodeList"
                :key="item.code"
                :value="'' + item.code">{{ item.name }}</Option>
            </Select>
          </FormItem>
          <FormItem prop="costUsed" label="费用用途">
            <Input v-model="form.data.costUsed" placeholder="请输入"></Input>
          </FormItem>
          <FormItem prop="costNum" label="数量">
            <InputNumber :max="9999" v-model="form.data.costNum" placeholder="请输入"></InputNumber>
          </FormItem>
          <FormItem prop="costCash" label="费用">
            <InputNumber :max="9999" v-model="form.data.costCash" placeholder="请输入"></InputNumber>
          </FormItem>
          <FormItem prop="costTime" label="费用产生时间">
            <DatePicker type="datetime" style="width:100%" placeholder="选择时间" v-model="form.data.costTime"></DatePicker>
          </FormItem>
          <FormItem prop="remark" label="费用说明">
            <Input type="textarea" v-model="form.data.remark" placeholder="请输入" :rows="4"></Input>
          </FormItem>
        </Form>
      </Col>
    </Row>
  </Modal>
</template>

<script>
  import { mapGetters } from 'vuex'
  import activityApi from '@/api/activity'

  export default {
    name: 'update-modal',
    props: [
      'form'
    ],
    data () {
      return {
        loading: true,
        ruleValidate: {
          costType: [
            { required: true, message: '请选择费用类型', trigger: 'change' }
          ],
          costUsed: [
            { max: 50, message: '费用说明字符长度应小于50', trigger: 'change' }
          ],
          costNum: [
            { required: true, type: 'number', message: '请输入数量', trigger: 'change' }
          ],
          costCash: [
            { required: true, type: 'number', message: '请输入费用', trigger: 'change' }
          ],
          costTime: [
            { required: true, type: 'date', message: '请选择费用产生时间', trigger: 'change' }
          ],
          remark: [
            { max: 100, message: '费用说明字符长度应小于100', trigger: 'change' }
          ]
        }
      }
    },
    computed: {
      ...mapGetters({
        userInfo: 'userInfo',
        costTypeCodeList: 'costTypeCodeList'
      })
    },
    watch: {
      'form.visible': 'onClose'
    },
    methods: {
      ok () {
        this.$refs.form.validate((valid) => {
          if (valid) {
            this.submitForm()
          } else {
            this.$Message.error('表单验证失败，请检查输入项目!')
            this.form.visible = true
            this.loading = false
            setTimeout(() => {
              this.loading = true
            }, 100)
          }
        })
      },
      onClose (isVisible) {
        if (!isVisible) {
          this.$refs.form.resetFields()
        }
      },
      async submitForm () {
        let activityId = this.$route.params.activityId
        this.form.data.pkActBaseInfo = activityId
        if (this.form.data.id) {
          const { body } = await activityApi.costDetail.update(this.form.data)
          if (this.$apiHelper.requestSuccessed(body.responseCode)) {
            this.$Message.info('修改成功')
          } else {
            setTimeout(() => {
              this.$Modal.error({
                title: '错误',
                content: body.desc,
                onOk: () => {}
              })
            }, 500)
          }
        } else {
          const { body } = await activityApi.costDetail.create(this.form.data)
          if (this.$apiHelper.requestSuccessed(body.responseCode)) {
            this.$Message.info('创建成功')
          } else {
            setTimeout(() => {
              this.$Modal.error({
                title: '错误',
                content: body.desc,
                onOk: () => {}
              })
            }, 500)
          }
        }
        this.form.visible = false
        this.$emit('submit')
      }
    },
    mounted () {
      this.$store.dispatch('getCode', 'cost_type')
    }
  }
</script>
